package com.encryption.service.ca.util;

import java.math.BigInteger;

/**
 * Hex��ݴ��?��import java.math.BigInteger;
 */
public class HexUtil {
    /**
     * HexString ���ֽ�����ת����Ŀǰ����ǩ���ı������Base64��
     * @param s
     * @return
     */
    public static byte[] hexStringToBytes(String s) {
        s = s.length() % 2 != 0 ? "0" + s.toUpperCase() : s.toUpperCase();
        char c = s.charAt(0);
        if ((c == '8') || (c == '9') || (c == 'A') || (c == 'B') || (c == 'C') || (c == 'D')
            || (c == 'E') || (c == 'F')) {
            s = "00" + s;
        }
        byte[] b1 = s.getBytes();
        byte[] b2 = new byte[b1.length / 2];
        int j = 0;
        for (int i = 0; i < b1.length; j++) {
            int a = 0;
            int b = 0;
            if ((b1[i] >= 48) && (b1[i] <= 57)) {
                a = b1[i] - 48;
            } else if ((b1[i] >= 65) && (b1[i] <= 70)) {
                a = b1[i] - 55;
            }

            if ((b1[(i + 1)] >= 48) && (b1[(i + 1)] <= 57))
                b = b1[(i + 1)] - 48;
            else if ((b1[(i + 1)] >= 65) && (b1[(i + 1)] <= 70)) {
                b = b1[(i + 1)] - 55;
            }

            b2[j] = (byte) ((a << 4) + b);

            i += 2;
        }
        return b2;
    }

    /**
     * Hexת��Ϊ�ֽ�����
     * @param s
     * @return
     */

    public static byte[] hexToBytes(String s) {
        s = s.length() % 2 != 0 ? "0" + s.toUpperCase() : s.toUpperCase();
        byte[] b1 = s.getBytes();
        byte[] b2 = new byte[b1.length / 2];
        int j = 0;
        for (int i = 0; i < b1.length; j++) {
            int a = 0;
            int b = 0;
            if ((b1[i] >= 48) && (b1[i] <= 57)) {
                a = b1[i] - 48;
            } else if ((b1[i] >= 65) && (b1[i] <= 70)) {
                a = b1[i] - 55;
            }
            if ((b1[(i + 1)] >= 48) && (b1[(i + 1)] <= 57))
                b = b1[(i + 1)] - 48;
            else if ((b1[(i + 1)] >= 65) && (b1[(i + 1)] <= 70)) {
                b = b1[(i + 1)] - 55;
            }
            b2[j] = (byte) ((a << 4) + b);

            i += 2;
        }
        return b2;
    }

    /**
     * ��BigIntegerת��Ϊ16�����ַ�
     *
     * @param bigInteger BigInteger����
     *
     * @return 16�����ַ�
     */
    public static String bigIntegerToHexString(BigInteger bigInteger) {
        byte[] bytes = bigInteger.toByteArray();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            int unitByte = bytes[i];

            if (unitByte < 0) {
                unitByte += 256;
            }
            StringBuffer hexUnitByte = new StringBuffer(Integer.toHexString(unitByte));
            if (hexUnitByte.length() < 2) {
                hexUnitByte.insert(0, "0");
            }
            stringBuffer.append(hexUnitByte);
        }
        return stringBuffer.toString();
    }
    
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }  

}
